import { CommonService } from './../../api/common.service';
import { ActivatedRoute } from '@angular/router'
import { Component, OnInit} from '@angular/core';
import { FormBuilder, FormControl, FormGroup, ValidationErrors, Validators } from '@angular/forms';
import { Router,NavigationExtras } from '@angular/router'
import { StorageService } from 'src/app/cache/storage.service';

@Component({
  selector: 'app-jpa',
  templateUrl: './jpa.component.html',
  styleUrls: ['./jpa.component.less'],
})
export class JpaComponent implements OnInit {

  // title
  title:any = '';
  validateForm: FormGroup;
  dataFlag:boolean=true;

  sqlinit:string="CREATE TABLE `tb_user` (\n" +
  "  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',\n" +
  "  `uuid` bigint NOT NULL COMMENT '雪花ID',\n" +
  "  `user_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '姓名',\n" +
  "  `phone` int NOT NULL COMMENT '手机号',\n" +
  "  `email` varchar(15) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '邮箱',\n" +
  "  PRIMARY KEY (`id`) USING BTREE\n" +
  ") ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='用户表';"

  autoCode:any={};


  submitForm(): void {
    this.commonHttp.postApiJson("/autoCode",this.validateForm.value).then((response:any) => {
      
        if(!response.success){
          alert(response.message)
        }
        this.storage.set("sqlinit",this.validateForm.value.sql);
        this.storage.set("authorName",this.validateForm.value.authorName);
        this.dataFlag = true;
        this.autoCode = response.data;
        // 将数据放入本地缓存以供其他组件使用
        this.storage.set("autoCode",this.autoCode);
        let queryParams:NavigationExtras={
          queryParams:{'jCodeInit':'entity'}
      }
        // 跳转的 showCode页面
        this.routes.navigate(['/showCode'],queryParams);
   
    });
 
  }

  resetForm(e: MouseEvent): void {
    e.preventDefault();
    this.validateForm.reset();
    for (const key in this.validateForm.controls) {
      console.log(key)
      if (this.validateForm.controls.hasOwnProperty(key)) {
        this.validateForm.controls[key].markAsPristine();
        this.validateForm.controls[key].updateValueAndValidity();
      }
    }
  }

 

  // eslint-disable-next-line @typescript-eslint/explicit-function-return-type

  constructor(
    public aroute:ActivatedRoute, 
    private fb: FormBuilder,
    public commonHttp:CommonService,
    public routes:Router,
    public storage:StorageService
    ) { 

    let sql = this.storage.get("sqlinit");
    let authorName = this.storage.get("authorName")==0?'':this.storage.get("authorName");
    if(sql == ""){
        sql = this.sqlinit;
    } 
    console.log(sql)
    this.validateForm = this.fb.group({
      sql: [sql, [Validators.required]],
      ignorePrefix: ['', [Validators.required]],
      authorName: [authorName, [Validators.required]]
    });

    this.dataFlag = false;
  }

  ngOnInit(): void {
       // 标题
       this.aroute.params.subscribe((data:any) => {
        this.title = data.type;
      });
  }


}
